// 生成UUID
export const getUuid = function(len, radix) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  var uuid = []; var i;
  radix = radix || chars.length;

  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  } else {
    var r;

    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
    uuid[14] = '4';

    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | Math.random() * 16;
        uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r];
      }
    }
  }
  return uuid.join('');
};
// https://www.wjx.cn/vj/YvmfXwr.aspx
// 随机一个英文字母
export const getRandomLetter = function() {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  const num = parseInt(Math.random() * chars.length);
  const letter = chars[num];
  return letter;
};

// 解析 python 代码中函数的参数列表
export const parseParameters = function(functionName, content) {
  const contentOfIndex = content;
  // 手动匹配，防止被注释部分影响
  for (var i = 0; i < content.length; i++) {
    if (content[i] === 'd' && content[i + 1] === 'e' && content[i + 2] === 'f' && (i === 0 || content[i - 1] === '\n')) {
      var k = i + 3;
      var str = '';
      while (content[k] !== '(') {
        str = str + content[k];
        k++;
      }
      if (str === functionName) {
        break;
      }
    }
  }
  var matc = '';
  if (contentOfIndex[k] !== '(') {
    k++;
  }
  k++;
  while (contentOfIndex[k] !== ')') {
    matc = matc + contentOfIndex[k];
    k++;
  }
  const paramsStr = matc;// match[1];
  console.log(paramsStr);
  var funcInfo = {};
  funcInfo['functionPrototype'] = paramsStr;
  funcInfo['introText'] = `函数的作用是...\n函数输入的变量有:\n分别用于...\n返回的变量有...\n分别代表...`;
  funcInfo['variables'] = paramsStr.split(',').map(param => {
    const parts = param.trim().split(':');
    var temType = parts[1] ? parts[1].trim() : ''; // 不显示声明则显示为空串，但是要求在UI界面填写
    const checkDefault = parts[0].trim().split('=');
    const paramName = checkDefault[1] ? checkDefault[0] : parts[0];
    const paramType = checkDefault[1] ? '(变量类型),可选的参数' : temType;
    const paramIntro = `这是${paramType || '...(变量类型)'}, 表示...的含义, 用于..., 默认值是...`;
    return {
      name: paramName,
      type: paramType,
      intro: paramIntro
    };
  });
  return funcInfo;
};
